//导入 axios
import axios from 'axios';
//导入 nprogress
import NProgress from 'nprogress';
//引入 nprogress.css
import 'nprogress/nprogress.css';
//导入 sweetalert2
import Swal from "sweetalert2";

//创建 axios 实例
let instance = axios.create({
  baseURL: 'http://localhost:8080'
});

//设置请求拦截器
instance.interceptors.request.use(config => {
  //开启loading
  NProgress.start();
  return config;
});

//设置响应拦截器
instance.interceptors.response.use(response => {
  //关闭 loading
  NProgress.done();
  //成功回调
  return response.data;
}, err => {
  //失败提醒
  Swal.fire({
    icon: "error",
    text: "请求失败.",
  });
  //抛出一个错误
  throw err;
});

//暴露
export default instance;